﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO = Microsoft.MSE.Catalog.Contracts;
using System.Xml;

namespace Microsoft.MSE.Catalog.Module.Data
{
    public class BindingElement
    {
        public static Guid CreateMSEBindingElement(DTO.MSEBindingElement bindingElement, SqlTransaction txn, SqlConnection conn)
        {
            Guid ID;
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[CreateBindingElement]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                    SqlParameter ElementConfigParam = new SqlParameter("@ElementConfig", SqlDbType.Xml);
                    SqlParameter ElementTypeParam = new SqlParameter("@ElementType", SqlDbType.TinyInt);
                    SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Direction = ParameterDirection.Output;
                    NameParam.Value = bindingElement.Name;
                    ElementTypeParam.Value = (int)bindingElement.ElementType;
                    if (bindingElement.BindingElementConfig != null)
                    {
                        ElementConfigParam.Value = bindingElement.BindingElementConfig.OuterXml;
                    }
                    else
                    {
                        throw new ApplicationException("CreateBinding failed: BindingConfig is a required element but was not sent");
                    }
                    if (bindingElement.Description != null)
                    {
                        DescriptionParam.Value = bindingElement.Description;
                    }
                    else
                    {
                        DescriptionParam.Value = DBNull.Value;
                    }

                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(ElementConfigParam);
                    cmd.Parameters.Add(ElementTypeParam);
                    cmd.Parameters.Add(DescriptionParam);

                    cmd.ExecuteNonQuery();

                    ID = (Guid)IDParam.Value;
                }
            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection in opened in this method
            }

            return ID;
        }

        public static DTO.MSEBindingElement UpdateMSEBindingElement(DTO.MSEBindingElement binding, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[UpdateBindingElement]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);
                    SqlParameter NameParam = new SqlParameter("@Name", SqlDbType.NVarChar, 400);
                    SqlParameter ElementConfigParam = new SqlParameter("@ElementConfig", SqlDbType.Xml);
                    SqlParameter ElementTypeParam = new SqlParameter("@ElementType", SqlDbType.TinyInt);
                    SqlParameter DescriptionParam = new SqlParameter("@Description", SqlDbType.NText);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = binding.ID;
                    NameParam.Value = binding.Name;
                    ElementTypeParam.Value = (int)binding.ElementType;
                    if (binding.BindingElementConfig != null)
                    {
                        ElementConfigParam.Value = binding.BindingElementConfig.OuterXml;
                    }
                    else
                    {
                        throw new ApplicationException("UpdateBinding failed: BindingConfig is a required element but was not sent");
                    }
                    if (binding.Description != null)
                    {
                        DescriptionParam.Value = binding.Description;
                    }
                    else
                    {
                        DescriptionParam.Value = DBNull.Value;
                    }

                    cmd.Parameters.Add(IDParam);
                    cmd.Parameters.Add(NameParam);
                    cmd.Parameters.Add(ElementConfigParam);
                    cmd.Parameters.Add(ElementTypeParam);
                    cmd.Parameters.Add(DescriptionParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection in opened in this method
            }

            return binding;
        }

        public static void DeleteMSEBindingElement(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            const string sprocname = "[ServiceEngine].[DeleteBindingElement]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    cmd.ExecuteNonQuery();
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection in opened in this method
            }
        }

        public static DTO.MSEBindingElement GetMSEBindingElement(Guid ID, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.MSEBindingElement binding = new DTO.MSEBindingElement();
            const string sprocname = "[ServiceEngine].[GetBindingElement]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter IDParam = new SqlParameter("@ID", SqlDbType.UniqueIdentifier);

                    cmd.CommandType = CommandType.StoredProcedure;
                    IDParam.Value = ID;
                    cmd.Parameters.Add(IDParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            reader.Read();
                            binding.ID = reader.GetGuid(0);
                            binding.Name = reader.GetString(1);
                            XmlDocument xdoc = new XmlDocument();
                            xdoc.Load(reader.GetSqlXml(2).CreateReader());
                            binding.BindingElementConfig = xdoc.DocumentElement;
                            if (reader.IsDBNull(3))
                            {
                                binding.Description = null;
                            }
                            else
                            {
                                binding.Description = reader.GetString(3);
                            }
                            binding.ElementType = (DTO.BindingElementType)reader.GetInt16(4);
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection in opened in this method
            }

            return binding;

        }

        public static List<DTO.MSEBindingElement> GetMSEBindingElementByName(string BindingElementName, SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            List<DTO.MSEBindingElement> list = null;
            const string sprocname = "[ServiceEngine].[GetBindingElementByName]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    SqlParameter BindingElementNameParam = new SqlParameter("@BindingElementName", SqlDbType.NVarChar, 400);

                    cmd.CommandType = CommandType.StoredProcedure;
                    BindingElementNameParam.Value = BindingElementName;
                    cmd.Parameters.Add(BindingElementNameParam);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list = new List<DTO.MSEBindingElement>();
                            while (reader.Read())
                            {
                                DTO.MSEBindingElement binding = new DTO.MSEBindingElement();
                                binding.ID = reader.GetGuid(0);
                                binding.Name = reader.GetString(1);
                                XmlDocument xdoc = new XmlDocument();
                                xdoc.Load(reader.GetSqlXml(2).CreateReader());
                                binding.BindingElementConfig = xdoc.DocumentElement;
                                if (reader.IsDBNull(3))
                                {
                                    binding.Description = null;
                                }
                                else
                                {
                                    binding.Description = reader.GetString(3);
                                }
                                binding.ElementType = (DTO.BindingElementType)reader.GetInt16(4);

                                list.Add(binding);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection in opened in this method
            }

            return list;

        }


        public static DTO.MSEBindingElementList ListMSEBindingElements(SqlTransaction txn, SqlConnection conn)
        {
            bool autoclose = false;
            DTO.MSEBindingElementList list = new DTO.MSEBindingElementList();
            const string sprocname = "[ServiceEngine].[ListBindingElements]";

            try
            {
                if (conn == null) conn = CatalogSvcHelper.GetConnection();
                if (conn.State == ConnectionState.Closed)
                {
                    autoclose = true;
                    conn.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sprocname, conn, txn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            list.MSEBindingElements = new List<DTO.MSEBindingElementListElement>();
                            while (reader.Read())
                            {
                                DTO.MSEBindingElementListElement element = new DTO.MSEBindingElementListElement();
                                element.ID = reader.GetGuid(0);
                                element.Name = reader.GetString(1);
                                if (reader.IsDBNull(2))
                                {
                                    element.Description = null;
                                }
                                else
                                {
                                    element.Description = reader.GetString(2);
                                }
                                list.MSEBindingElements.Add(element);
                            }
                        }
                    }
                }

            }
            finally
            {
                if (autoclose && (conn != null)) conn.Close(); //Close connection in opened in this method
            }


            return list;

        }

    }
}
